import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
import path = require('path');

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1655089002668_2288';

  // add your egg config in here
  config.middleware = [];

  // config.redis = {
  //   client: {
  //     port: 6379, // Redis port
  //     host: '127.0.0.1', // Redis host
  //     password: '',
  //     db: 0,
  //   },
  // };

  config.mongoose = {
    clients: {
      // clientId, access the client instance by app.mongooseDB.get('clientId')
      pl24: {
        url: 'mongodb://127.0.0.1/pl24',
        options: {
          useUnifiedTopology: true,
        },
        // client scope plugin array
      },
    },
    // public scope plugin array
  };

  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
  };

  // the return config will combines to EggAppConfig
  return {
    ...config,
    ...bizConfig,
    static: {
      prefix: '/',
    },
    view: {
      root: [
        path.resolve(__dirname, '../app/public'),
        path.resolve(__dirname, '../app/view'),
      ].join(','), // 将 app/public 目录作为模板目录
      mapping: {
        // 映射配置，将不同的模板后缀映射到对应的模板引擎处理
        '.ejs': 'ejs',
      },
      defaultViewEngine: 'ejs', // 如果映射找不到对应的模板引擎，将使用该值作为默认使用的模板引擎
      defaultExtension: '.ejs', // 后续在controller中渲染模板时，默认渲染的模板后缀名
    },
    security: {
      csrf: {
        enable: false,
      },
    },
  };
};
